//
//  SCjCollectionViewLayout.m
//  15-homework
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SCjCollectionViewLayout.h"

@interface SCjCollectionViewLayout ()

@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArray;
@property (nonatomic, assign) CGFloat contentWidth;

@end
@implementation SCjCollectionViewLayout

- (instancetype)init {
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
        _cellHeight = 30;
        _cellMargin = 30;
        _contentWidth = 15;
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        _attributesArray = [NSMutableArray array];
        _cellHeight = 30;
        _cellMargin = 30;
        _contentWidth = 15;
    }
    return self;
}
- (void)prepareLayout {
    
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        CGFloat x = _contentWidth;
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self height:_cellHeight sizeForItemAtIndexPath:index];
        attribute.frame = CGRectMake(x, 0, width, _cellHeight);
        [_attributesArray addObject:attribute];
        _contentWidth = _contentWidth +  _cellMargin + width;
    }
}
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSLog(@"%@", _attributesArray);    
    return _attributesArray;
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(_contentWidth, 0);
}



@end
